DataTable = {}
local tableMultiId = {

}
local function setMetatableConfigTable(name,originTable)
    local keys = originTable["_key_"]
    local __keys = {}
    for k,v in pairs(keys) do
        __keys[v] = k
    end
    local meta = {
        __index = function(t,k)
            local ks =  rawget(t,"__keys")
            local index = ks[k]
            if index == nil then return nil end
            local value = t[index]
            return value
        end
    }
    for k,v in pairs(originTable) do
        v["__keys"] = __keys
        setmetatable(v,meta)
    end
end
local function LoadDataTable( name)
    local sTable = require("Data/"..name)
    local newTable = {}
    setMetatableConfigTable(name,sTable)
    local keyList = tableMultiId[name]
    for k,v in pairs(sTable) do
        if k ~= "_key_" then
            local t = v
            local cache
            if keyList ~= nil then
                if #keyList == 0 then
                    newTable[k] = t
                else
                    for i,j in pairs(keyList) do
                        if cache == nil then
                            cache = newTable
                        end
                        if i == #keyList then
                            cache[t[j]] = t
                        else
                            if cache[t[j]] == nil then
                                cache[t[j]] = {}
                            end
                            cache = cache[t[j]]
                        end
                    end
                end

            else
                newTable[k] = t
            end
        end
    end
    DataTable[name] = newTable
    for k,v in pairs(sTable) do
        sTable[k] = nil
    end
    package.loaded["Data/"..name] = nil;
    _G["Data/"..name] = nil
    return newTable
end

function DataTable.Get( table_name )
    table_name = string.lower(table_name)
    local ret = DataTable[table_name]
    if ret == nil then
        ret = LoadDataTable(table_name)
    end
    if ret then return ret end
end
function DataTable.GetById( table_name,id,columnName)
    local cfgs = DataTable.Get(table_name)
    if cfgs then
        if columnName then
            local c = cfgs[tonumber(id)]
            if not c then return nil end
            return c[columnName]
        else
            return cfgs[tonumber(id)]
        end
    end
    return nil
end
function DataTable.GetByMultiId( table_name,id1,id2,id3)
    local cfgs = DataTable.Get(table_name)
    if cfgs then
        if id1 then
            local c = cfgs[id1]
            if id2 then
                if c then
                    local c2 = c[id2]
                    if id3 then
                        return c2[id3]
                    else
                        return c2
                    end
                else
                    return nil
                end
            else
                return c
            end
        else
            return cfgs
        end

    end
    return nil
end
